package util;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;

/**
 * @author tg.si@188.com
 * @todo
 * @since 2025/3/26 14:20
 */
@Setter
@ToString
@Getter
public class Condition {
    public static String toWhereClause(List<Condition> conditionList){
        StringBuilder sb = new StringBuilder(" WHERE ");
        for(Condition condition : conditionList){
            sb.append(condition.getKey());
            sb.append(" ");
            sb.append(condition.getOperator());
            sb.append(" ");
            String value = condition.getValue();
            if(condition.getOperator().equals("like")||
                    condition.getOperator().equals("LIKE")){
                value="%"+value+"%" ;
                value=TxtHelper.getSingleQuoteMarked(value);

            }
            sb.append(value);
            sb.append(" and ");

        }
        TxtHelper.truncEnding(sb,4);
        return sb.toString();
    }
    public Condition(String key, String operator, String value) {
        this.key = key;
        this.operator = operator;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public String getOperator() {
        return operator;
    }

    public String getValue() {
        return value;
    }

    public String toString() {
        return key + " " + operator + " " + value;
    }

    // 字段名
    private String key;
    // 操作符
    private String operator;
    // 字段值
    private String value;
}